// pages/signUp-detail/signUp-detail.js
const app = getApp()
var util = require('../../util/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId:'',
    activity:{},
    _createTime:'',
    peopleList:[],
    pointOut: 0
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      activityId: options.activityId 
    })
    this._getDetail()
  },



  _getDetail() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })

    wx.cloud.callFunction({
      name: 'signUp',
      data: {
        activityId: this.data.activityId,
        $url: 'detail'
      }
    }).then((res) => {
      console.log(res.result)
      let activity = res.result.detail[0]
      activity.createTime = util.formatTime(new Date(activity.createTime))
      let ab=0;
      for (let i = 0; i < res.result.peopleList.data.length; i++) {
        console.log(res.result.peopleList.data[i])
        if (res.result.peopleList.data[i]._openid == app.globalData.openid) {
          ab = ab + 1
        }
      }
      this.setData({
        activity: activity,
        peopleList: res.result.peopleList.data,
        pointOut: ab
      })
      console.log(activity)
      console.log(this.data.peopleList)
      wx.hideLoading()

    })
  },

  goSignUp(){
    console.log(util.compareTime(this.data.activity.auto))
    if (util.compareTime(this.data.activity.auto)=='已关闭'){
      wx.showModal({
        title: '活动已截止',
        content: '',
      })
      return
    }
    console.log(this.data.activity.snum)
    console.log(this.data.peopleList)
    let abs=0;
    if (this.data.activity.snum[1]!=0){
      for (let i = 0; i < this.data.peopleList.length; i++){
        if (this.data.peopleList[i]._openid == app.globalData.openid){
          abs=abs+1
        }
      }
      if (abs >= this.data.activity.snum[1]){
        wx.showModal({
          title: '代报次数已用完',
          content: '',
        })
        return
      }
    }
    if (this.data.peopleList.length >= this.data.activity.snum[0] && this.data.activity.snum[0]!=0) {
      wx.showModal({
        title: '已满人',
        content: '',
      })
      return
    }

    console.log(app.globalData.openid)
    let formArr = JSON.stringify(this.data.activity.formArr);
    let snum = JSON.stringify(this.data.activity.snum);
    wx.navigateTo({
      url: `../to-signUp/to-signUp?activityId=${this.data.activityId}&formArr=${formArr}&snum=${snum}`,
    })
  },

  delActive(){
    if(this.data.activity._openid == app.globalData.openid){
    wx.showModal({
      title: '提示',
      content: '确定要删除该活动吗？',
      success: (res)=> {
        if (res.confirm) {
          console.log(res)
          wx.cloud.callFunction({
            name: "signUp",
            data: {
              activeId: this.data.activityId,
              $url: 'onDel'
            },
            success: res => {
              wx.showToast({
                title: '删除成功！',
              })
              
              wx.hideLoading();
              wx.navigateBack()
              const pages = getCurrentPages()
              const prePage = pages[pages.length - 2]
            },
            fail: err => {
              wx.showToast({
                title: '[云函数]失败' + err,
              })
              console.error('[云函数]调用失败', err)
            }
          })

        } else if (res.cancel) {
          return false;
        }}})}else{
      wx.showModal({
        title: '提示',
        content: '确定要删除该活动吗？',
        success: (res) => {
          if (res.confirm) {
            console.log(res)
            wx.cloud.callFunction({
              name: "signUp",
              data: {
                activeId: this.data.activityId,
                $url: 'delUser'
              },
              success: res => {
                wx.showToast({
                  title: '删除成功！',
                })

                wx.hideLoading();
                wx.navigateBack()
                const pages = getCurrentPages()
                const prePage = pages[pages.length - 2]
              },
              fail: err => {
                wx.showToast({
                  title: '[云函数]失败' + err,
                })
                console.error('[云函数]调用失败', err)
              }
            })

          } else if (res.cancel) {
            return false;
          }
        }
      })
        }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._getDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (event) {
    console.log(event.target.dataset.activity)
    let activityObj = event.target.dataset.activity
    return {
      title: activityObj.title,
      path: `/pages/signUp-detail/signUp-detail?activityId=${activityObj._id}`,
      //imageUrl:''
    }
  }
})